using System;
using System.Runtime.InteropServices;
using System.Text;

namespace GA_ScreenScraper
{
    public class Win32
    {
        // WIN32 Window Messages
        public const int WM_GETTEXT = 0xD;
        public const int WM_GETTEXTLENGTH = 0xE;

        //WIN32 ListBox messages
        public const int LB_GETCOUNT = 0x18B;
        public const int LB_GETTEXTLEN = 0x18A;
        public const int LB_GETTEXT = 0x189;

        // The FindWindow and FindWindowEx methods can be used to find the handle of a window
        // given its caption or its window class name. For more information, see MSDN.
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,
                                                 string lpszWindow);

        // EnumChildWindows is used to enumerate all the childeren of the given parent window handle
        [DllImport("User32.dll")]
        public static extern Boolean EnumChildWindows(IntPtr hWndParent, Delegate lpEnumFunc, int lParam);

        // getClassName returns the window class name for the provided window handle
        [DllImport("User32.Dll")]
        public static extern void GetClassName(IntPtr hWnd, StringBuilder s, int nMaxCount);
        
        // getClassName returns the window class name for the provided window handle
        [DllImport("User32.Dll")]
        public static extern IntPtr GetDlgCtrlID(IntPtr hWnd);
                
        // SendMessage allows us to send various Win32 Windows messages to any window of our choice
        // We have overloaded it to accept a StrinBuilder as one of its arguments
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
                                                [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam);
        
        public static int HiWord(int number)
        {
            if ((number & 0x80000000) == 0x80000000)
                return (number >> 16);
            else
                return (number >> 16) & 0xffff;
        }

        public static int LoWord(int number)
        {
            return number & 0xffff;
        }

        public static int MakeLong(int LoWord, int HiWord)
        {
            return (HiWord << 16) | (LoWord & 0xffff);
        }

        public static IntPtr MakeLParam(int LoWord, int HiWord)
        {
            return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
        }
    }
}